//
//  WeatherForecastHeadView.m
//  DLTravels
//
//  Created by mac1012 on 2016/12/19.
//  Copyright © 2016年 DianlE. All rights reserved.
//

#import "WeatherForecastHeadView.h"
#import "WeatherLayer.h"


@interface WeatherForecastHeadView ()
{
    UIImageView *_levelImageV;
    UIView  *_contentView;
    WeatherLayer *_weatherLayer;
}

@end

@implementation WeatherForecastHeadView

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
        CGFloat margin = 20;
        CGFloat viewH = kScreen_Width/2;
        _levelImageV = [[UIImageView alloc] initWithFrame:CGRectMake(margin, margin, viewH-margin*2.5, 180-margin*2.5)];
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(viewH, 0, viewH, 180)];
        [self addSubview:_levelImageV];
        [self addSubview:_contentView];
        
        _levelImageV.image = [UIImage imageNamed:@"weatherLevel"];
       
        [self setupContentSubView];
        [self startAnimation];
        
    }
    
    return self;
}

- (void)setupContentSubView{

//    CGFloat padding = 8;
    CGFloat margin = 20;
    CGFloat imgH = 25;
    CGFloat labelWidth = CGRectGetWidth(_contentView.frame) - imgH;
    CGFloat labelHeight = imgH;
    WeatherLayer *weatherLayer = [[WeatherLayer alloc] init];
    weatherLayer.frame = CGRectMake(0, margin, imgH, imgH);
    weatherLayer.image = [UIImage imageNamed:@"windDirection"];
    
    
    UILabel *windLevelTitle = [[UILabel alloc]initWithFrame:CGRectMake(imgH, margin, labelWidth, labelHeight)];
    windLevelTitle.text = @"微风";
    windLevelTitle.textColor = [UIColor whiteColor];
    windLevelTitle.textAlignment = NSTextAlignmentCenter;
    windLevelTitle.font = [UIFont systemFontOfSize:12.0];
    
    [_contentView addSubview:windLevelTitle];
    NSArray *imageNames = @[@"windForce",@"sunrise"];
    NSArray *labelTitles = @[@"无持续风向",@"07:22-18:22"];
    for (NSInteger index = 0; index < 2; index ++) {
        CGFloat orginY = CGRectGetMinY(weatherLayer.frame)+CGRectGetHeight(weatherLayer.frame);
        CGFloat subMargin = index == 0 ?margin:margin*(index+1);
        UIImageView *windForceImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, orginY+subMargin+(index)*imgH, imgH, imgH)];
        windForceImgV.image = [UIImage imageNamed:imageNames[index]];
        
        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(windForceImgV.frame), CGRectGetMinY(windForceImgV.frame), labelWidth, labelHeight)];
        labelTitle.text = labelTitles[index];
        labelTitle.textColor = [UIColor whiteColor];
        labelTitle.textAlignment = NSTextAlignmentCenter;
        labelTitle.font = [UIFont systemFontOfSize:12.0];
        
        [_contentView addSubview:windForceImgV];
        [_contentView addSubview:labelTitle];
    }
    
//    UIImageView *windForceImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(weatherLayer.frame)+CGRectGetHeight(weatherLayer.frame)+margin, imgH, imgH)];
//    windForceImgV.image = [UIImage imageNamed:@"windForce"];
//    
//    UIImageView *sunriseImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(windForceImgV.frame)+CGRectGetHeight(windForceImgV.frame)+margin, imgH, imgH)];
//    sunriseImgV.image = [UIImage imageNamed:@"sunrise"];
    
    
    [_contentView.layer addSublayer:weatherLayer];
//    [_contentView addSubview:windForceImgV];
//    [_contentView addSubview:sunriseImgV];
    _weatherLayer = weatherLayer;
}

- (void)startAnimation{

    CAAnimation *animation = [_weatherLayer animationForKey:@"weatherLayerAnimation"];
    if (animation == nil) {
        
        CABasicAnimation *basicAnimation = [CABasicAnimation animation];
        basicAnimation.keyPath = @"transform.rotation.z";
        basicAnimation.toValue = @(M_PI);
        basicAnimation.cumulative = YES;    //循环动画在如为yes 在上次结束时跟上, 如果为NO 从开始Value重新动画
        basicAnimation.duration = 3.0;
        
        basicAnimation.repeatCount = MAXFLOAT;
        [_weatherLayer addAnimation:basicAnimation forKey:@"weatherLayerAnimation"];
        
    }
    
}

@end
